<?php

/**
 * Created by PhpStorm.
 * User: issuser
 * Date: 2017/7/4
 * Time: 下午2:28
 */
class ChatApi
{
    /**
     * @param $params
     * @param $user
     * @return mixed
     */
    public static function messageReceived($params, $user){

        error_log('_messageReceived start');
        $content = json_decode($params["content"], true);
        $text = $content["_lctext"];

        $processedContent = self::repl("../resources/敏感词库.txt",$text);

        error_log("文字替换成功:$processedContent");

        $tempContent = $content;
        $tempContent["_lctext"] = $processedContent;
        $content = json_encode($tempContent, true);
        $params["content"] = $content;
        return $params;
    }

    //"../resources/敏感词库.txt"
    private static function repl ($name, $text){

        $filterFile = fopen($name, "r");
        $filterText = fgets($filterFile);
        fclose($filterFile);

        $filterArr = json_decode($filterText, true);

        $processedContent = preg_replace($filterArr, "**", $text);
        return $processedContent;
    }
}


//
//$filterFile = fopen("../resources/敏感词源文件.txt", "r");
//
//$filterText = fgets($filterFile);
//$count = 0;
//
//$arr = array();
//
//while ($filterText){
//
//    $filterText = preg_replace("/\r/", "", $filterText);
//    $filterText = preg_replace("/\n/", "", $filterText);
//
//    $arr[$count] = "/$filterText/";
//
//    $count = $count + 1;
//    $filterText = fgets($filterFile);
//}
//
//fclose($filterFile);
//
//$jsonText = json_encode($arr, true);
//
//echo $jsonText;
//
//$myfile = fopen("/Users/issuser/Desktop/testfile.txt", "w");
//
//fwrite($myfile, $jsonText);
//fclose($myfile);
